package javamail.speech;

import com.sun.speech.freetts.*;
import javax.sound.sampled.*;
import com.sun.speech.freetts.audio.*;

/**
 *
 * @author Admin
 */

public class speechSynthesizer {
      private Voice voice;
/*
 * The method below generate sound from txt and save to file
 * msg:- txt that to be generated.
 * filename:- the  file name of generated sound   
 * no return
 */
    public void synthesize(String msg, String fileName)
    {
          voice=VoiceManager.getInstance().getVoice("kevin");
          AudioFileFormat.Type type = AudioFileFormat.Type.WAVE;
          SingleFileAudioPlayer sfap = new SingleFileAudioPlayer(formatFileName(fileName), type);
          if(voice!=null && sfap!=null) {
              voice.allocate();
              voice.setVolume(1.0f);
              voice.setAudioPlayer(sfap);
              voice.speak(msg);
              voice.deallocate();
              sfap.close();
          } else
              System.err.println("GenerateSound::GeneAudio-Unable to create Voice object");
    }
    public String formatFileName(String filename) {
        String ext = ".wav";
        int index = 0;
        if((index=filename.indexOf(ext))>=0)
            return filename.substring(0, index);
        return filename;
    }
}
